package com.me.finance.session;

import java.io.Serializable;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

import com.me.finance.dao.BasicSessionDAO;
import com.me.finance.entity.User;

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class UserSession implements Serializable {

	private static final long serialVersionUID = 1L;
	
	@EJB
	private BasicSessionDAO dao;
	private String query;
	
	public UserSession() {	}
	
	public User saveUser(User user) {
		dao.save(user);
		return user;
	}
	
	public User updateUser(User user) {
		dao.update(user);
		return user;
	}
	
	public void removeUser(User user) {
		dao.remove(user);
	}
	
	public List<User> getAllUsers() {
		query = "SELECT u FROM User u ORDER BY u.name";
		return dao.getList(User.class, query);
	}

	public User getUserById(int userId) {
		return dao.getSingle(User.class, userId);
	}
	
	public List<User> getUsersByName(String userName){
		query = "SELECT u FROM User u WHERE u.nome LIKE ?1";
		return dao.getList(User.class, query, userName+"%");
	}
	
	public User getUserByLogin(String userLogin) {
		query = "SELECT u FROM User u WHERE u.login = ?1";
		return dao.getSingle(User.class, query, userLogin);
	}

}
